home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 051-075 / scopedisk75 / ispell / src / lookup.c < prev    next >
C/C++ Source or Header  |  1995-03-19  |  2KB  |  82 lines

  1. /* -*- Mode:Text -*- */
  2. /*
  3.  * lookup.c - see if a word appears in the dictionary
  4.  *
  5.  * Pace Willisson, 1983
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. #include "ispell.h"
  11.  
  12.  
  13. struct dent *hashtbl;
  14. int hashsize;
  15.  
  16. static inited = 0;
  17.  
  18. linit ()
  19. {
  20.     int hashfd;
  21.     struct hashheader hashheader;
  22.     char hashname[100];
  23.  
  24.     strcpy (hashname, LIBDIR);
  25.  
  26. #if USEDEVICES
  27.     strcat (hashname, "ispell.hash");
  28. #else
  29.     strcat (hashname, "/ispell.hash");
  30. #endif
  31.  
  32.     if (inited)
  33.         return;
  34.  
  35.     if ((hashfd = open ("ispell.hash", 0)) < 0 &&
  36.         (hashfd = open (hashname, 0)) < 0) {
  37.         fprintf (stderr, "can't open %s\r\n", hashname);
  38.         return (-1);
  39.     }
  40.  
  41.     read (hashfd, &hashheader, sizeof hashheader);
  42.  
  43.     if (hashheader.magic != MAGIC) {
  44.         fprintf (stderr, "Illegal format hash table\r\n");
  45.         return (-1);
  46.     }
  47.     hashstrings = (char *) malloc (hashheader.stringsize);
  48.     hashtbl = (struct dent *) malloc (hashheader.tblsize * sizeof (struct dent));
  49.     hashsize = hashheader.tblsize;
  50.  
  51.     read (hashfd, hashstrings, hashheader.stringsize);
  52.     read (hashfd, hashtbl, hashheader.tblsize * sizeof (struct dent));
  53.     close (hashfd);
  54.  
  55.     inited = 1;
  56.     return (0);
  57. }
  58.  
  59. /* n is length of s */
  60. struct dent *
  61. lookup (s, n)
  62. register char *s;
  63. {
  64.     register int i;
  65.     register struct dent *dp;
  66.     register char *s1, *s2;
  67.  
  68.     for (i = hash (s, n, hashsize); i > 0; i = (int)(dp->next)) {
  69.         dp = &hashtbl[i];
  70.         /* quick strcmp, but only for equality */
  71.         s1 = &hashstrings [ (int)(dp->word) ];
  72.         s2 = s;
  73.         while (*s1 == *s2++)
  74.             if (*s1++=='\0') {
  75.                 lastdent = &hashtbl[i];
  76.                 return (lastdent);
  77.             }
  78.     }
  79.     return (NULL);
  80. }
  81.  
  82.